﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Xml;
using System.Reflection;
using System.IO;
 
namespace DataProvider
{
    public static class SqlConfig
    {
        private const String lockStr = "";
        private static Dictionary<String, SqlBlock> sqlPool = new Dictionary<string, SqlBlock>(10240,StringComparer.OrdinalIgnoreCase);

        static SqlConfig()
        {
            lock (lockStr)
            {
                ReadSqlConfig();
            }
        }

        private static void ReadSqlConfig()
        {
             //To Do
        }

        private static void ReadSqlItem(String sqlXml)
        {
            using (XmlReader reader = new XmlTextReader(sqlXml, XmlNodeType.Element, null))
            {
                reader.MoveToContent();
                do
                {
                    if (String.Compare(reader.Name, "Sql", true) == 0)
                    {
                        SqlBlock block = new SqlBlock();
                        block.Id = reader.GetAttribute("Id");
                        block.SqlType = (SqlType)Enum.Parse(typeof(SqlType), reader.GetAttribute("SqlType"));
                        block.ResultClass = reader.GetAttribute("ResultClass");
                        block.Sql = reader.ReadString();

                        try
                        {
                            sqlPool.Add(block.Id, block);
                        }
                        catch (Exception e)
                        {
                            DataLayerLog.LogError(e.Message + "ID:" + block.Id);
                            throw e;
                        }
                    }
                } while (reader.Read());
            }
        }

        public static SqlBlock GetSqlBlock(String id)
        {
            if (sqlPool.ContainsKey(id))
            {
                return sqlPool[id].CloneSelf();
            }
            else
            {
                throw new Exception("ID:" + id +"的Sql语句不存在");
            }
        }

    }
}
